/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.enums.EmployeeEnums;
import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.EmployeeHelper;
import com.eyaomall.wxmall.common.util.StringFilterUtil;
import com.eyaomall.wxmall.model.po.Employee;
import com.eyaomall.wxmall.model.po.Goods;
import com.eyaomall.wxmall.model.po.Shop;
import com.eyaomall.wxmall.model.po.Stores;
import com.eyaomall.wxmall.model.vo.EmployeesDepositVO;
import com.eyaomall.wxmall.model.vo.GoodsVO;
import com.eyaomall.wxmall.service.CategoryService;
import com.eyaomall.wxmall.service.EmployeeService;
import com.eyaomall.wxmall.service.EmployessDepositService;
import com.eyaomall.wxmall.service.GoodsImgService;
import com.eyaomall.wxmall.service.GoodsService;
import com.eyaomall.wxmall.service.ShopService;
import com.eyaomall.wxmall.service.SmsService;
import com.eyaomall.wxmall.service.StoresService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.common.cookie.SessionManager;
import com.lycheeframework.core.exception.ErrorCodeException;
/**
 * 店员
 * @author pro
 *
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private StoresService storesService;
	
	@Autowired
	private SmsService smsService;
	
	@Autowired
	private EmployessDepositService depositService;
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private GoodsImgService goodsImgService;
	
	@Autowired
	private CategoryService categoryService;
	
	/**
	 * 店员首页
	 * @return
	 */
	@RequestMapping("index")
	public String index(Map<String,Object> data){
		data.put("name", EmployeeHelper.getName());
		data.put("phone", EmployeeHelper.getPhone());
		data.put("storeName", EmployeeHelper.getStoreName());
		data.put("totalAmount", depositService.sum(EmployeeHelper.getId()));
		
		return "employee/index";
	}
	
	/**
	 * 登录
	 * @return
	 */
	@RequestMapping("login")
	public String login(){
		
		return "employee/login";
	}
	
	/**
	 * 登录
	 * @param phone
	 * @param passwd
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("login_data")
	public @ResponseBody GsnData loginData(String phone,String passwd,HttpServletRequest request,HttpServletResponse response){
		if(StringUtil.isEmpty(phone) || StringUtil.isEmpty(passwd))
			throw new ErrorCodeException(BECode.employee_8000001);
		
		String shopId = (String) request.getSession().getAttribute("employee_shop_id");
		if(StringUtil.isEmpty(shopId))
			throw new ErrorCodeException(BECode.employee_8000007);
		
		passwd = EmployeeHelper.getEntryPasswd(passwd);
		Employee employee = employeeService.login(phone,passwd);
		if(employee == null)
			throw new ErrorCodeException(BECode.employee_8000000);

		Stores stores = storesService.queryById(employee.getStoreId());
		if(stores == null)
			throw new ErrorCodeException(BECode.employee_8000009);
		
		if(Integer.parseInt(shopId) != stores.getShopId()){
			throw new ErrorCodeException(BECode.employee_8000006);
		}
		
		SessionManager manager = new SessionManager(request);
		manager.setValue(EmployeeEnums._EMPLOYEE_FROM.getName(), EmployeeEnums._EMPLOYEE_FROM_ID.getName(), String.valueOf(employee.getId()),true);
		manager.setValue(EmployeeEnums._EMPLOYEE_FROM.getName(), EmployeeEnums._EMPLOYEE_FROM_NAME.getName(), employee.getName(),true);
		manager.setValue(EmployeeEnums._EMPLOYEE_FROM.getName(), EmployeeEnums._EMPLOYEE_FROM_PHONE.getName(), employee.getPhone(),true);
		
		manager.setValue(EmployeeEnums._EMPLOYEE_FROM.getName(), EmployeeEnums._EMPLOYEE_FROM_SHORES.getName(), String.valueOf(stores.getId()),true);
		manager.setValue(EmployeeEnums._EMPLOYEE_FROM.getName(), EmployeeEnums._EMPLOYEE_FROM_SHORES_NAME.getName(), stores.getName(),true);
		manager.save(request, EmployeeEnums._EMPLOYEE_FROM.getName(),true);
		
		return GsnResponse.toData(true);
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	@RequestMapping("change_passwd")
	public String changePass(){
		
		return "employee/change_passwd";
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	@RequestMapping("save_new_passwd")
	public @ResponseBody GsnData saveNewPass(String phone,String passwd,String oldPasswd,HttpServletRequest request){
		if(StringUtil.isEmpty(phone))
			throw new ErrorCodeException(BECode.employee_8000002);
		
		if(StringUtil.isEmpty(passwd))
			throw new ErrorCodeException(BECode.employee_8000004);
		
		if(StringUtil.isEmpty(oldPasswd))
			throw new ErrorCodeException(BECode.employee_8000005);
		
		Employee employee = employeeService.login(phone, EmployeeHelper.getEntryPasswd(oldPasswd));
		if(employee == null)
			throw new ErrorCodeException(BECode.employee_8000000);
		
		employee.setPasswd(EmployeeHelper.getEntryPasswd(passwd));
		boolean flag = employeeService.update(employee);
		if(flag){
			EmployeeHelper.cleanAll();

			Stores stores = storesService.queryById(EmployeeHelper.getStoreId());
			if(stores != null){
				EmployeeHelper.cleanAll();
				System.out.println("ttttt" + stores.getShopId());
				request.getSession().setAttribute("employee_shop_id",String.valueOf(stores.getShopId()));
			} 
		}
		
		return GsnResponse.toData(flag);
	}
	
	/**
	 * 忘记密码页面
	 * @return
	 */
	@RequestMapping("forget_page")
	public String forgetPage(){
		
		return "employee/forget";
	}
	
	/**
	 * 忘记密码
	 * @param phone
	 * @return
	 */
	@RequestMapping("forget")
	public @ResponseBody GsnData forget(String phone,Map<String,Object> data,HttpServletRequest request){
		if(StringUtil.isEmpty(phone))
			throw new ErrorCodeException(BECode.employee_8000002);
		
		Employee employee = employeeService.queryByPhone(phone);
		if(employee == null)
			throw new ErrorCodeException(BECode.employee_8000003);
		
		Stores stores = storesService.queryById(employee.getStoreId());
		if(stores == null)
			throw new ErrorCodeException(BECode.employee_8000009);
		
		String sId = (String) request.getSession().getAttribute("employee_shop_id");
		if(StringUtil.isNotEmpty(sId)){
			if(!sId.equalsIgnoreCase(stores.getShopId().toString())){
				throw new ErrorCodeException(BECode.employee_8000008);
			}
		}
		
		String oriPasswd = RandomStringUtils.random(6, "abcdefghijklmnopqrstuvwxyz1234567890");
		employee.setPasswd(EmployeeHelper.getEntryPasswd(oriPasswd));
		boolean flag = employeeService.update(employee);
		if(flag){
			smsService.resetpasswd(phone, oriPasswd);
		}
		
		return GsnResponse.toData(true);
	}
	
	/**
	 * 加载我的业绩
	 * @return
	 */
	@RequestMapping("load_deposit")
	public @ResponseBody GsnData loadDeposit(PageHTTPObject httpObject){
		EmployeesDepositVO deposit = new EmployeesDepositVO();
		deposit.setEmployeeId(EmployeeHelper.getId());
		
		return GsnResponse.toData(depositService.page(deposit, httpObject));
	}
	
	/**
	 * 我要推广列表
	 * @return
	 */
	@RequestMapping("goods_list")
	public String goodsList(String serach,Map<String, Object> map){
		serach=StringFilterUtil.SimpleStringFilter(serach);
		map.put("serach", serach);
		Stores stores = storesService.queryById(EmployeeHelper.getStoreId());
		if(stores != null){
			map.put("category", categoryService.queryByShopId(stores.getShopId()));
		}
		
		return "employee/goods_list";
	}
	
	/**
	 * 我要推广列表
	 * @return
	 */
	@RequestMapping("goods_detail")
	public String goodsDetail(Integer id,Map<String, Object> map){
		Goods goods = goodsService.query(id);//根据传入的Id查询商品
		Shop shop = shopService.queryById(goods.getShopId());//查询店铺信息
		map.put("img", goodsImgService.queryImgByGoodsId(id));
		map.put("goods",goods);
		map.put("shop",shop);
		map.put("eId", EmployeeHelper.getId());
		
		return "employee/goods_detail";
	}
	
	/**
	 * 分页显示商品列表
	 */
	@RequestMapping("page_data")
	public @ResponseBody GsnData pageData(GoodsVO goodsVO,PageHTTPObject httpObject){
		Stores stores = storesService.queryById(EmployeeHelper.getStoreId());
		goodsVO.setShopId(stores.getShopId());
		goodsVO.setIsPublish("y");
		return GsnResponse.toData(goodsService.page(goodsVO, httpObject));
	}
	
	/**
	 * 获取微信公众号js签名
	 * @param openId
	 * @param url
	 * @param token
	 * @return
	 */
	@RequestMapping("create_sign")
	public @ResponseBody GsnData createSign(String url){
		Integer storeId = EmployeeHelper.getStoreId();
		Stores stores = storesService.queryById(storeId);
		Shop shop = shopService.queryById(stores.getShopId());
		
		Map<String,String> sign = HomeController.sign(shop.getJsTicket(), url);
		sign.put("appId", shop.getWxAppId());
		return GsnResponse.toData(sign);
	}
}
